# 输入：读取11_labeled.xls的三个列(内容分类,信访概况,难度),文件位置"../data/11_labeled.xls"
# 输出：由于NER模型对长句子未训练，所以输出到Excel中手动完善(信访概况)，文件位置"./Excel_to_be_optimized.xls"
# 注意！注意！对Excel完成(信访概况)的手工优化后,要对其(信访概况列)进行手动排序,再执行To_Library文件,否则难度规则库会错误。
# 注意！注意！对Excel完成(信访概况)的手工优化后,要对其(信访概况列)进行手动排序,再执行To_Library文件,否则难度规则库会错误。
import json
import pandas as pd
import requests
import xlrd
import xlwt
# 注意！注意！对Excel完成(信访概况)的手工优化后,要对其(信访概况列)进行手动排序,再执行To_Library文件,否则难度规则库会错误。
# 注意！注意！对Excel完成(信访概况)的手工优化后,要对其(信访概况列)进行手动排序,再执行To_Library文件,否则难度规则库会错误。
post_url = "http://36.7.159.235:10015/role"
classification_list = []  # 内容分类列表
content_list = []  # 内容列表 此为NER模型的输入
difficulty_list = []  # 难度列表
incident_list = []  # 实体事件列表

workbook = xlrd.open_workbook("../data/11_labeled.xls",encoding_override="utf-8")
table1 = workbook.sheets()[0]
for i in range(2, table1.nrows):
    # 内容分类
    Content_classification = table1.cell_value(i, 11)  # 内容分类
    classification_list.append(Content_classification)
    # 信访概况列表
    content = table1.cell_value(i, 12)  # 信访概况
    content_list.append(content)
    # 难度值列表
    difficulty = table1.cell_value(i, 16)  # 难度
    difficulty_list.append(difficulty)

# for items in classification_list:


print("content_list的长度：", len(content_list))
# print(content_list)

# 调用NER模型接口
for i in content_list:
    count = 0
    request_param = {"string": i}
    # post请求
    response = requests.post(post_url, request_param)
    load_data = json.loads(response.text)  # 将响应内容转换为Json对象
    # print("当前是第", content_list.index(i) + 1, "个信访概况", "分段后的实体个数：", len(load_data['text']), end=", ")
    # 如果没有返回的实体，则输出“无返回的text”
    if len(load_data['text']) == 0:
        # print("无返回的text", end=", ")
        # incident_list.append("无返回的text")
        incident_list.append(i)
        # continue
    # 如果有返回的实体，则搜索是否有INCIDENT
    else:
        for index in range(len(load_data['text'])):
            if load_data['text'][index]['type'] == "INCIDENT":
                count = count + 1
                # print("count", count, end=", ")
                # print("INCIDENT:", load_data['text'][index]['word'], end=" ")
                incident_list.append(load_data['text'][index]['word'])
                break
        if count == 0:
            # print("count", count, end=", ")
            # print("有text,无incdent", end=" ")
            # incident_list.append("有text,无incdent")
            incident_list.append(i)
        else:
            # print("count", count, end=", ")
            count = 0

    # print("难度：", difficulty_list[content_list.index(i)])

print(len(incident_list), " ", len(difficulty_list))
# print(incident_list)
# print(difficulty_list)

writebook = xlwt.Workbook(encoding='utf-8')
# 创建一个sheet对象,第二个参数是指单元格是否允许重设置，默认为False
sheet = writebook.add_sheet('sheet1', cell_overwrite_ok=True)

# for i in range(len(incident_list)):
#     sheet.write(i, 0, incident_list[i])

test = pd.DataFrame({'classification_list': classification_list, '实体事件': incident_list, '难度': difficulty_list})
# test.to_json("./test.pkl")
test.to_excel("./Excel_to_be_optimized.xls")
